import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-06-11
 * Time: 14:04
 */
public class MyStack {
    private int[] elem;
    private int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    // 压栈
    public void push(int val) {
        if (isFull()) {
            // 扩容
            this.elem = Arrays.copyOf(this.elem, this.elem.length*2);
        }
        this.elem[usedSize] = val;
        this.usedSize++;
    }
    // 判断栈是否满了
    public boolean isFull() {
        return usedSize == elem.length;
    }
    // 出栈
    public int pop() {
        if(isEmpty()) {
            throw new StackEmptyException("栈为空!");
        }
        return this.elem[--usedSize];
    }
    // 判断栈是不是为空
    public boolean isEmpty() {
        return usedSize == 0;
    }
    // 获取栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new StackEmptyException("栈为空!");
        }
        // 开始删除
        return this.elem[usedSize--];
    }


}
